var AES_KEY = '8NONwyJtHesysWpM';
var ENCODE_KEY = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
var SSESSION_KEY;

function generateSessionKey() {
    if (!SSESSION_KEY) {
        SSESSION_KEY = 'web-' + new Date().getTime() + Math.round(Math.random() * 10000);
    }
    return SSESSION_KEY;
}

function setCookie(name, value, days) {
    days = days || 30;
    var exp = new Date();
    exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
    document.cookie = name + '=' + escape(value) + ';expires=' + exp.toUTCString();
}

function Encrypt(content){
    var key = CryptoJS.enc.Utf8.parse(AES_KEY);
    var srcs = CryptoJS.enc.Utf8.parse(content);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        mode:CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
}

function Decrypt(content){
    var key = CryptoJS.enc.Utf8.parse(AES_KEY);
    var decrypt = CryptoJS.AES.decrypt(content, key, {
        mode:CryptoJS.mode.ECB,
        padding: CryptoJS.pad.Pkcs7
    });
    return CryptoJS.enc.Utf8.stringify(decrypt).toString();
}

// 将Ansi编码转换成Base64编码
function encode64(input) {
    var output = '';
    var chr1, chr2, chr3 = '';
    var enc1, enc2, enc3, enc4 = '';
    var i = 0;
    do {
        chr1 = input.charCodeAt(i++);
        chr2 = input.charCodeAt(i++);
        chr3 = input.charCodeAt(i++);
        enc1 = chr1 >> 2;
        enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
        enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
        enc4 = chr3 & 63;
        if (isNaN(chr2)) {
            enc3 = enc4 = 64;
        } else if (isNaN(chr3)) {
            enc4 = 64;
        }
        output = output + ENCODE_KEY.charAt(enc1) + ENCODE_KEY.charAt(enc2)
            + ENCODE_KEY.charAt(enc3) + ENCODE_KEY.charAt(enc4);
        chr1 = chr2 = chr3 = '';
        enc1 = enc2 = enc3 = enc4 = '';
    } while (i < input.length);
    return output;
}

// 将Base64编码转换成Ansi编码
function utf16to8(str) {
    var out, i, len, c;
    out = '';
    len = str.length;
    for (i = 0; i < len; i++) {
        c = str.charCodeAt(i);
        if ((c >= 0x0001) && (c <= 0x007F)) {
            out += str.charAt(i);
        } else if (c > 0x07FF) {
            out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
            out += String.fromCharCode(0x80 | ((c >> 6) & 0x3F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        } else {
            out += String.fromCharCode(0xC0 | ((c >> 6) & 0x1F));
            out += String.fromCharCode(0x80 | ((c >> 0) & 0x3F));
        }
    }
    return out;
}

// 十六进制转换成2进制编码
function hex_to_bin(str) {
    var hex_array = [{
            key:0, val:'0000'
        },{
            key:1, val:'0001'
        },{
            key:2, val:'0010'
        },{
            key:3, val:'0011'
        },{
            key:4, val:'0100'
        },{
            key:5, val:'0101'
        },{
            key:6, val:'0110'
        },{
            key:7, val:'0111'
        },{
            key:8, val:'1000'
        },{
            key:9, val:'1001'
        },{
            key:'a', val:'1010'
        },{
            key:'b', val:'1011'
        },{
            key:'c', val:'1100'
        },{
            key:'d', val:'1101'
        },{
            key:'e', val:'1110'
        },{
            key:'f', val:'1111'
        }];

    var value = '';
    for (var i = 0; i < str.length; i++) {
        for (var j = 0; j < hex_array.length; j++) {
            if (str.charAt(i) == hex_array[j].key) {
                value = value.concat(hex_array[j].val);
                break;
            }
        }
    }
    return value;
}